package com.qf.cloud2202.microb.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.qf.cloud2202.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class TestController {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    EurekaClient eurekaClient;

    @Autowired
    RestTemplate restTemplate;


    @GetMapping("/test2")
    public String test2(){

        final InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("MICROA", false);

        final String url = instanceInfo.getHomePageUrl();

        // get 请求
        final String result = restTemplate.getForObject(url + "test1", String.class);

        return result;

    }

    @GetMapping("/test3")
    public String test3(){

        final InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("MICROA", false);

        final String url = instanceInfo.getHomePageUrl();

        // 发送get请求
        final String result = restTemplate.getForObject(url + "user/getall", String.class);

        return result;

    }


    @GetMapping("/test4")
    public String test4(){

        final InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("MICROA", false);

        final String url = instanceInfo.getHomePageUrl();

        final User user = new User(200,"zhangsan");

        // 第二个参数，代表post请求体中放的数据
        final String result = restTemplate.postForObject(url+ "user/update", user, String.class);

        return result;

    }

    @GetMapping("/test5")
    public String test5(){

        final InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("MICROA", false);

        final String url = instanceInfo.getHomePageUrl();

        restTemplate.delete(url+"user/delete/{id}",100);

        return "ok";

    }


    @GetMapping("/test6/{id}")
    public User test6(@PathVariable("id") Integer id){

        final InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("MICROA", false);

        final String url = instanceInfo.getHomePageUrl();

        /**
         *
         * 第三个参数，封装请求头和请求体中的数据，可以为空
         *
         * 第五个参数，有没有uri路径参数需要设置，替换规则，把{}中的内容一次替换
         */
        final ResponseEntity<User> responseEntity = restTemplate.exchange(url + "user/delete/{id}", HttpMethod.DELETE, null,
                User.class, id);

        final User user = responseEntity.getBody();

        return user;

    }


    @GetMapping("/test7")
    public User test6(){

        final InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("MICROA", false);

        final String url = instanceInfo.getHomePageUrl();

        final User user = new User(300,"luffy");
        final HttpEntity<User> userHttpEntity = new HttpEntity<>(user);


        final ResponseEntity<User> responseEntity = restTemplate.exchange(url + "user/update", HttpMethod.POST, userHttpEntity,
                User.class);

        return responseEntity.getBody();

    }


}
